package cn.gson.fengboot.model.service;

import cn.gson.fengboot.model.dao.TfyclientFormDao;
import cn.gson.fengboot.model.dao.TfyclueFormDao;
import cn.gson.fengboot.model.dao.TfyempoperationDao;
import cn.gson.fengboot.model.dao.TfylinkmanFormDao;
import cn.gson.fengboot.model.mapper.TfyclientMapper;
import cn.gson.fengboot.model.mapper.TfyclueMapper;
import cn.gson.fengboot.model.pojos.*;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class TfyClueService {

    @Autowired
    TfyclueFormDao tfyclueFormDao;//线索

    @Autowired
    TfyclientFormDao tfyclientFormDao;//客户

    @Autowired
    TfylinkmanFormDao tfylinkmanFormDao;//联系人

    @Autowired
    TfyclueMapper tfyclueMapper;//线索mapper

    @Autowired
    TfyclientMapper tfyclientMapper;//客户mapper

    //线索对客户多对一、模糊查询
    public List<TfyclueForm> findAllClue(String search){
        return tfyclueMapper.findAllClue(search);
    }

    //已失效线索对客户多对一、模糊查询
    public List<TfyclueForm> findAllCluezt(String search){
        return tfyclueMapper.findAllCluezt(search);
    }

    //分配给我的线索查询
    public List<TfyclueForm> findClueClient(String search){
        System.out.println("s"+search);
        return tfyclueMapper.findClueClient(search);
    }

    //线索新增方法
    public void addclue(TfyclueForm xs){
        xs.setClueState(1);
        xs.setClueIffollowup(0);
        TfyclueForm save = tfyclueFormDao.save(xs);
    }

    //逻辑删除线索
    public void deleteClue(String cluedx){
        JSONObject obj = (JSONObject)JSONObject.parse(cluedx);
        List cluelist = obj.getObject("cluelist",List.class);
        //转化成JSONArray
        JSONObject object = null;
        JSONArray json = JSONArray.parseArray(String.valueOf(cluelist));
        System.out.println("集合："+cluelist.toString());
        for(int i=0; i<json.size(); i++){
            object = (JSONObject)json.get(i);
            Integer clueid = object.getInteger("clueId");
            System.out.println("线索ID："+clueid);
            System.out.println("obj:"+obj.toString());
            TfyclueForm tfyclueForm = tfyclueFormDao.findById(clueid).get();
            System.out.println("线索:" + tfyclueForm.toString());
            tfyclueForm.setClueState(0);
            TfyclueForm save = tfyclueFormDao.save(tfyclueForm);
        }
    }

    //恢复线索修改
    public void hfClue(String cluedx){
        JSONObject obj = (JSONObject)JSONObject.parse(cluedx);
        List cluelist = obj.getObject("cluelist",List.class);
        //转化成JSONArray
        JSONObject object = null;
        JSONArray json = JSONArray.parseArray(String.valueOf(cluelist));
        System.out.println("集合："+cluelist.toString());
        for(int i=0; i<json.size(); i++){
            object = (JSONObject)json.get(i);
            Integer clueid = object.getInteger("clueId");
            System.out.println("线索ID："+clueid);
            System.out.println("obj:"+obj.toString());
            TfyclueForm tfyclueForm = tfyclueFormDao.findById(clueid).get();
            System.out.println("线索:"+tfyclueForm.toString());
            tfyclueForm.setClueState(1);
            TfyclueForm save = tfyclueFormDao.save(tfyclueForm);
        }
    }

    //物理删除线索
    public void scClue(String cluedx){
        JSONObject obj = (JSONObject)JSONObject.parse(cluedx);
        List cluelist = obj.getObject("cluelist",List.class);
        //转化成JSONArray
        JSONObject object = null;
        JSONArray json = JSONArray.parseArray(String.valueOf(cluelist));
        System.out.println("集合："+cluelist.toString());
        for(int i=0; i<json.size(); i++){
            object = (JSONObject)json.get(i);
            Integer clueid = object.getInteger("clueId");
            System.out.println("线索ID："+clueid);
            System.out.println("obj:"+obj.toString());
            TfyclueForm tfyclueForm = tfyclueFormDao.findById(clueid).get();
            System.out.println("线索:"+tfyclueForm.toString());
            tfyclueFormDao.delete(tfyclueForm);
        }
    }

    //根据线索id查询对应线索和客户方法
    public TfyclueForm findXskh(Integer clueId){
        return tfyclueMapper.findXskh(clueId);
    }

    //修改线索跟进状态
    public void gjztClue(String cluedx){
        JSONObject obj = (JSONObject)JSONObject.parse(cluedx);
        List cluelist = obj.getObject("cluelist",List.class);
        //转化成JSONArray
        JSONObject object = null;
        JSONArray json = JSONArray.parseArray(String.valueOf(cluelist));
        System.out.println("集合："+cluelist.toString());
        for(int i=0; i<json.size(); i++){
            object = (JSONObject)json.get(i);
            Integer clueid = object.getInteger("clueId");
            System.out.println("线索ID："+clueid);
            System.out.println("obj:"+obj.toString());
            TfyclueForm tfyclueForm = tfyclueFormDao.findById(clueid).get();
            System.out.println("线索:"+tfyclueForm.toString());
            tfyclueForm.setClueIffollowup(1);
            tfyclueFormDao.save(tfyclueForm);
        }
    }

    //线索 ->新增为客户和联系人
    public void pinkclient(TfyclueForm xs){
        TfyclientForm kh = new TfyclientForm();
        kh.setClientName(xs.getClueName());
        kh.setClientSource(xs.getClueSource());
        kh.setEmpId(xs.getEmpId());
        kh.setClientPhone(xs.getCluePhone());
        kh.setClientSite(xs.getClueSite());
        kh.setEstablishPerson(xs.getClueEstablishtime());
        kh.setClientRemark(xs.getClueRemark());
        kh.setClientSz(xs.getClueSz());
        kh.setClientState(1);
        kh.setClientIffollowup(0);
        kh.setClientPayattention("加关注");
        kh.setClientKhzt("潜在客户");
        TfyclientForm save = tfyclientFormDao.save(kh);

        TfylinkmanForm lxr = new TfylinkmanForm();
        lxr.setLinkmanName(xs.getClueLinkmanname());
        lxr.setLinkmanMobile(xs.getClueLinkmansj());
        lxr.setLinkmanState(1);
        lxr.setClientId(kh);
        lxr.setEmpId(xs.getEmpId());
        TfylinkmanForm save2 = tfylinkmanFormDao.save(lxr);
        xs.setClueState(0);
        xs.setClueIffollowup(1);
        tfyclueFormDao.save(xs);
    }

}
